/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.webapp.action;

import com.click.app.model.MaterialReturn;
import com.click.app.service.GenericManager;
import java.util.List;

/**
 *
 * @author orgale
 */
public class MaterialReturnAction extends BaseAction {

    private GenericManager<MaterialReturn, Long> materialReturnManager;
    private List materialReturns;
    private MaterialReturn materialReturn;
    private Long id;

    public void setId(Long id) {
        this.id = id;
    }

    public MaterialReturn getMaterialReturn() {
        return materialReturn;
    }

    public void setMaterialReturn(MaterialReturn materialReturn) {
        this.materialReturn = materialReturn;
    }

    public String delete() {
        materialReturnManager.remove(new Long(materialReturn.getIdMaterialReturn()));
        saveMessage(getText("materialReturn.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            materialReturn = materialReturnManager.get(id);
        } else {
            materialReturn = new MaterialReturn();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (materialReturn.getIdMaterialReturn() == null);

        materialReturn = materialReturnManager.save(materialReturn);

        String key = (isNew) ? "materialReturn.added" : "materialReturn.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setMaterialReturnManager(GenericManager<MaterialReturn, Long> materialReturnManager) {
        this.materialReturnManager = materialReturnManager;
    }

    public List getMaterialReturns() {
        return materialReturns;
    }

    public String list() {
        materialReturns = materialReturnManager.getAll();
        return SUCCESS;
    }
}